package com.example.demo.service.impl;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.Perms;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author admin
 * @version 1.0 <br/>
 * <p style="font-family:SimSun;font-weight:bold">时间:<br/>2021/12/1 21:57</p>
 * @description
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public void register(User user) {
        //处理业务
        //1.生成随机盐
        String salt = SaltUtils.getSalt(6);
        //将随机盐保存到user中
        user.setSalt(salt);
        //明文密码加密 md5+salt+hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        //将加密的密码添加到user中
        user.setPassword(md5Hash.toHex());
        userDao.save(user);
    }

    @Override
    public User findUser(String name) {
        return userDao.findUser(name);
    }

    @Override
    public User findRolesByUserName(String username) {
        return userDao.findRolesByUserName(username);
    }

    @Override
    public List<Perms> findPermsByRoleId(Integer id) {
        return userDao.findPermsByRoleId(id);
    }
}
